package drongo.desktop.window
{
	import flash.desktop.NativeApplication;
	import flash.display.NativeMenu;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.NativeWindowRenderMode;
	import flash.display.NativeWindowSystemChrome;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.NativeWindowBoundsEvent;
	import flash.events.NativeWindowDisplayStateEvent;
	import flash.system.Capabilities;
	
	public class DGNativeWindow
	{
		private var _transparent:Boolean;
		private var _resizable:Boolean;
		private var _maximizable:Boolean;
		private var _minimizable:Boolean;
		private var _title:String;
		private var _nativeWindow:NativeWindow;
		/**
		 * 系统窗口
		 * @param width
		 * @param heigth
		 * @param openTransparent
		 * @param window
		 * @param resizable
		 * @param maximizable
		 * @param minimizable
		 */
		public function DGNativeWindow(title:String,width:Number=600,heigth:Number=400,openTransparent:Boolean=false,resizable:Boolean=true,maximizable:Boolean=true,minimizable:Boolean=true)
		{
			super();
			this._title=title;
			this.width=width;
			this.height=heigth;
			this._transparent=openTransparent;
			this._resizable=resizable;
			this._maximizable=maximizable;
			this._minimizable=minimizable;
			createChildren();
		}
		private function createChildren():void{
			var options:NativeWindowInitOptions = new NativeWindowInitOptions;
			options.renderMode=NativeWindowRenderMode.DIRECT;
			options.resizable = this._resizable;
			options.maximizable = this._maximizable;
			options.minimizable = this._minimizable;
			if(this._transparent){
				options.systemChrome=NativeWindowSystemChrome.NONE;
			}else{
				options.systemChrome=NativeWindowSystemChrome.STANDARD;
			}
			options.transparent=this._transparent;
			
			this._nativeWindow=new NativeWindow(options);
			_nativeWindow.title=this._title;
			_nativeWindow.width=this._width;
			_nativeWindow.height=this._height;
			_nativeWindow.x=(Capabilities.screenResolutionX-this._width)/2;
			_nativeWindow.y= (Capabilities.screenResolutionY-this._height)/2;
			
			//events
			_nativeWindow.addEventListener(Event.ACTIVATE, nativeWindow_activateHandler, false, 0, true);
			_nativeWindow.addEventListener(Event.DEACTIVATE, nativeWindow_deactivateHandler, false, 0, true);
			
			_nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGING,dispatchEvent);
			_nativeWindow.addEventListener(NativeWindowDisplayStateEvent.DISPLAY_STATE_CHANGE,dispatchEvent);
//			_nativeWindow.addEventListener(Event.CLOSING, windowClosing_Handler);
			_nativeWindow.addEventListener(Event.CLOSE, windowClose_handler, false, 0, true);
			_nativeWindow.addEventListener(NativeWindowBoundsEvent.MOVING, dispatchEvent);
			_nativeWindow.addEventListener(NativeWindowBoundsEvent.MOVE, dispatchEvent);
			_nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZING, dispatchEvent);
			_nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZE, window_resizeHandler);
		}
		/**
		 * 窗口真正关闭时 
		 * @param e
		 */		
		protected function windowClose_handler(e:Event):void{
			close();
		}
		/**
		 * 代表 AIR 应用程序的 NativeApplication 对象。 
		 * @return
		 */		
		public function get nativeApplication():NativeApplication
		{
			return NativeApplication.nativeApplication;
		}
		/**
		 * 激活该窗口
		 */
		public function open():void{
			_nativeWindow.activate();
		}
		/**
		 * 关闭窗口 
		 */
		public function close():void{
			if(!_nativeWindow.closed){
				_nativeWindow.close();
			}
			if(nativeApplication.openedWindows.length==0){
				nativeApplication.exit();
			}
		}
		public function get width():Number { return _width; }
		private var _width:Number;
		public function set width(value:Number):void
		{
			if (_width == value)
				return;
			_width = value;
			if(_nativeWindow){
				_nativeWindow.width=_width;
			}
		}
		/**
		 * 高度 
		 * @return
		 */
		public function get height():Number { return _height; }
		private var _height:Number;
		public function set height(value:Number):void
		{
			if (_height == value)
				return;
			_height = value;
			if(_nativeWindow){
				_nativeWindow.height=_height;
			}
		}
		private var _menu:NativeMenu;
		public function get menu():NativeMenu { return _menu; }
		
		public function set menu(value:NativeMenu):void
		{
			if (_menu == value)
				return;
			_menu = value;
		}
		/**
		 * 当前窗口所在舞台
		 * @return 
		 */		
		public function get stage():Stage{
			return this._nativeWindow.stage;
		}
		private var _data:Object;
		public function get data():Object { return _data; }
		
		public function set data(value:Object):void
		{
			if (_data == value)
				return;
			_data = value;
		}
		/**
		 * 是否在显示状态 
		 * @return 
		 */		
		public function get active():Boolean{
			return 	_nativeWindow.active;
		}
		protected function nativeWindow_activateHandler(e:Event):void{
			dispatchEvent(new Event(Event.ACTIVATE));
		}
		protected function nativeWindow_deactivateHandler(e:Event):void{
			dispatchEvent(new Event(Event.DEACTIVATE));
		}
		protected function dispatchEvent(e:Event):void{
			
		}
		protected function window_resizeHandler(e:Event):void{
			
		}
	}
}